package com.web.document.controller;

import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.web.common.document.api.DocumentInfoApi;
import com.web.common.document.entity.DocumentChapterEntity;
import com.web.common.document.entity.DocumentContent;
import com.web.common.document.entity.DocumentContentEntity;
import com.web.common.document.entity.DocumentInfoEntity;
import com.web.common.document.entity.ShareDocInfo;
import com.web.common.result.R;
import com.web.common.result.ResultEnum;
import com.web.document.service.impl.DocumentInfoServiceImpl;
import com.web.service.filter.xss.XssIgnore;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * <p>
 * 文档信息 前端控制器
 * </p>
 *
 * @author zhouhui
 * @since 2023-12-04
 */
@Tag(name = "文档信息")
@Controller
@RequestMapping("/doc/info")
public class DocumentInfoController implements DocumentInfoApi {
	
	@Autowired
	private DocumentInfoServiceImpl documentInfoServiceImpl;

	@Operation(summary = "获取文档信息列表")
	@Override
	public R<IPage<DocumentInfoEntity>> getDocInfoList(DocumentInfoEntity condition) {
		return documentInfoServiceImpl.getDocInfoList(condition);
	}
	
	@Operation(summary = "获取文档信息详情")
	@Override
	public R<DocumentInfoEntity> getDocInfoDetail(DocumentInfoEntity condition) {
		return documentInfoServiceImpl.getDocInfoDetail(condition);
	}

	@Operation(summary = "保存文档信息")
	@Override
	public R<Object> saveDocInfo(DocumentInfoEntity info) {
		return documentInfoServiceImpl.saveDocInfo(info);
	}

	@Operation(summary = "更新文档信息")
	@Override
	public R<Object> updateDocInfo(DocumentInfoEntity info) {
		if(info.getMainId() == null) {
			return R.fail(null, ResultEnum.PARAMETER_ERROR);
		}
		return documentInfoServiceImpl.updateDocInfo(info);
	}

	@Operation(summary = "删除文档信息")
	@Override
	public R<Object> deleteDocInfo(DocumentInfoEntity info) {
		if(info.getMainId() == null) {
			return R.fail(null, ResultEnum.PARAMETER_ERROR);
		}
		return documentInfoServiceImpl.deleteDocInfo(info);
	}

	@Operation(summary = "保存文档章节信息")
	@Override
	public R<Object> saveChapter(DocumentChapterEntity chapter) {
		return documentInfoServiceImpl.saveChapter(chapter);
	}
	
	@Operation(summary = "删除章节信息")
	@Override
	public R<Object> deleteChapter(DocumentChapterEntity chapter) {
		if(chapter.getMainId() == null) {
			return R.fail(null, ResultEnum.PARAMETER_ERROR);
		}
		return documentInfoServiceImpl.deleteChapter(chapter);
	}
	
	@Operation(summary = "更新章节信息")
	@Override
	public R<Object> updateChapter(DocumentChapterEntity chapter) {
		if(chapter.getMainId() == null) {
			return R.fail(null, ResultEnum.PARAMETER_ERROR);
		}
		if(!StringUtils.hasText(chapter.getTitle())) {
			return R.fail(null, ResultEnum.PARAMETER_ERROR);
		}
		return documentInfoServiceImpl.updateChapter(chapter);
	}

	@XssIgnore
	@Operation(summary = "保存/更新文档内容")
	@Override
	public R<Object> saveContent(DocumentContentEntity content) {
		return documentInfoServiceImpl.saveContent(content);
	}

	@Operation(summary = "自动取消到期的分享内容")
	@Override
	public R<Object> autoCancelShare(String inner) {
		return documentInfoServiceImpl.autoCancelShare();
	}

	@Operation(summary = "根据文档Id获取章节列表")
	@Override
	public R<ShareDocInfo> getChapterList(DocumentChapterEntity chapter) {
		if(chapter.getInfoId() == null) {
			return R.fail(null, "未选择文档Id");
		}
		return documentInfoServiceImpl.getChapterList(chapter);
	}

	@Operation(summary = "根据内容索引获取文档内容")
	@Override
	public R<DocumentContent> getDocumentConent(DocumentContentEntity content) {
		if(content.getChapterId() == null) {
			R.fail(null, "未选择章节Id");
		}
		return documentInfoServiceImpl.getDocumentConent(content);
	}
}
